/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tmp.ac.lib.tools;

import ac.uitls.propertiesOperation;
import java.awt.event.MouseAdapter;
import java.sql.Array;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author ismail
 */
public class custemItmes {

    DBClass db = new DBClass();
    propertiesOperation po = new propertiesOperation();

    public static String getComBoxValue(JComboBox com) {

        String returnData = "";
        try {
            tmp.ac.lib.tools.Item itemCate = (tmp.ac.lib.tools.Item) com.getSelectedItem();
            returnData = itemCate.getId();
        } catch (Exception ex) {
        }



        return returnData;
    }

    public static void setComBoxSelectonByValue(JComboBox com, String value) {


        for (int i = 0; i < com.getItemCount(); i++) {

            tmp.ac.lib.tools.Item itemCate = (tmp.ac.lib.tools.Item) com.getItemAt(i);
            
            
           // uiUtil.sendMessage(null, itemCate.getDescription(), value);
            if (itemCate.getId().equals(value)) {
                com.setSelectedIndex(i);
            }



        }


    }

    public void fillCombox(JComboBox com, String table, String valueFiled, String TextFilde) {
        Vector model = new Vector();
        db.setOpenCon();
        ResultSet rs = db.getDataWithOutCon(table);
        try {
            while (rs.next()) {
                model.addElement(new Item(rs.getString(valueFiled), rs.getString(TextFilde)));
            }
        } catch (SQLException ex) {
            Logger.getLogger(custemItmes.class.getName()).log(Level.SEVERE, null, ex);
        }
        com.setModel(new DefaultComboBoxModel(model));
        db.setCloseCon();
    }

    public void fillCombox(JComboBox com, String[] data) {
        Vector model = new Vector();
        for (String d : data) {
            model.addElement(new Item(d, d));
        }
        com.setModel(new DefaultComboBoxModel(model));

    }

    public void fillCombox(JComboBox com, String[] value, String[] text) {
        Vector model = new Vector();
        for (int i = 0; i < text.length; i++) {
            model.addElement(new Item(value[i], text[i]));
        }
        com.setModel(new DefaultComboBoxModel(model));
    }

    public static void setJlistelectonByValue(JList com, String value) {

        String[] Values = value.split(";");



        ArrayList<Integer> ftd = new ArrayList<Integer>();
        for (int i = 0; i < Values.length; i++) {

            for (int x = 0; x < com.getModel().getSize(); x++) {
                // uiUtil.sendMessage(null,""+ Values[i].trim(),""+ com.getModel().getElementAt(x));

                if (Values[i].trim().equals(com.getModel().getElementAt(x))) {
                    ftd.add(x);
                }
            }
        }
        
        
        
        int[] ii=new int[ftd.size()];
        for(int hh=0;hh<ftd.size();hh++){
        ii[hh]=ftd.get(hh);
        }
        
      
      
        com.setSelectedIndices(ii);
        /* uiUtil.sendMessage(null,""+ com.getModel().getSize(),""+ com.getModel().getSize());

       

         for (int i = 0; i <= Values.length; i++) {

         // JList.Values[i]);
         if (!Values[i].trim().equals("")) {
         for (int x = 0; x < com.getModel().getSize(); x++) {
         if (Values[i].equals(com.getModel().getElementAt(x))) {
         //   uiUtil.sendMessage(null, "" + com.getModel().getSize(), "x" + com.getModel().getElementAt(x));


         //com.setSelectedIndex(x);
         }
         }
         }
         }*/
    }

    public static String getListValue(JList com) {

        // ac.lib.tools.Item itemCate = (ac.lib.tools.Item) com.getSelectedItem();
        String returnData = "";
        for (int i = 0; i < com.getSelectedValuesList().size(); i++) {

            returnData += com.getSelectedValuesList().get(i) + ";";

        }

        return returnData;

    }

    public static void setListSelectonByValue(JList com, String value) {

        /*
         for (int i = 0; i < com.getItemCount(); i++) {

         ac.lib.tools.Item itemCate = (ac.lib.tools.Item) com.getSelectedItem();
         if (itemCate.getId().equals(value)) {
         com.setSelectedIndex(i);
         }



         }*/
    }

    public void fillList(JList com, String table, String valueFiled, String TextFilde) {
        Vector model = new Vector();
        db.setOpenCon();
        ResultSet rs = db.getDataWithOutCon(table);
        try {
            while (rs.next()) {
                model.addElement(new Item(rs.getString(valueFiled), rs.getString(TextFilde)));


            }
        } catch (SQLException ex) {
            Logger.getLogger(custemItmes.class
                    .getName()).log(Level.SEVERE, null, ex);
        }
        com.setModel(new DefaultComboBoxModel(model));
        db.setCloseCon();
    }

    public void fillList(JList com, String[] data) {
        Vector model = new Vector();
        for (String d : data) {
            model.addElement(new Item(d, d));
        }
        com.setModel(new DefaultComboBoxModel(model));

    }

    public void fillList(JList com, String[] value, String[] text) {
        Vector model = new Vector();
        for (int i = 0; i < text.length; i++) {
            model.addElement(new Item(value[i], text[i]));
        }
        com.setModel(new DefaultComboBoxModel(model));
    }

    public void TabelUpdat(JTable jTable1, ResultSet rs, String[] cols, String[] Rows, int[] hideCols) {
        DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
        model.setRowCount(0);
        model.setColumnIdentifiers(cols);

        //  uiUtil.sendMessage(jTable1, "", null);
        try {

            while (rs.next()) {
                Object[] n = new Object[Rows.length];
                for (int i = 0; i < Rows.length; i++) {
                    n[i] = rs.getString(Rows[i]);
                }
                model.addRow(n);
            }
        } catch (SQLException ex) {
            uiUtil.sendMessage(null, po.getWord("title.error"), ex.getMessage());
        }

        jTable1.setModel(model);
        model.fireTableDataChanged();
        jTable1.repaint();
        // revalidate();
        for (int i = 0; i < hideCols.length; i++) {
            jTable1.getColumnModel().getColumn(hideCols[i]).setMinWidth(0);
            jTable1.getColumnModel().getColumn(hideCols[i]).setMaxWidth(0);
            jTable1.getColumnModel().getColumn(hideCols[i]).setPreferredWidth(0);
        }
    }

    public void fahres(final JTable jtable, final JLabel tableLabelData, JButton frist, JButton perv, JButton next, JButton last) {

        jtable.getSelectionModel().setSelectionInterval(0, 0);
        tableLabelData.setText((jtable.getSelectedRow() + 1) + "/" + jtable.getRowCount());
        frist.addMouseListener(new MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {

                jtable.getSelectionModel().setSelectionInterval(0, 0);
                tableLabelData.setText((jtable.getSelectedRow() + 1) + "/" + jtable.getRowCount());
                // jTable1.
            }
        });


        last.addMouseListener(new MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtable.getSelectionModel().setSelectionInterval(jtable.getRowCount() - 1, jtable.getRowCount() - 1);
                tableLabelData.setText((jtable.getSelectedRow() + 1) + "/" + jtable.getRowCount());
                // jTable1.
            }
        });





        next.addMouseListener(new MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {

                int sel = jtable.getSelectedRow();
                if (sel == jtable.getRowCount() - 1) {
                    jtable.getSelectionModel().setSelectionInterval(0, 0);
                } else if (sel == -1) {
                    jtable.getSelectionModel().setSelectionInterval(jtable.getRowCount() - 1, jtable.getRowCount() - 1);
                } else {

                    jtable.getSelectionModel().setSelectionInterval(sel + 1, sel + 1);

                }


                tableLabelData.setText((jtable.getSelectedRow() + 1) + "/" + jtable.getRowCount());
                // jTable1.
            }
        });




        perv.addMouseListener(new MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                int sel = jtable.getSelectedRow();
                if (sel == 0) {
                    jtable.getSelectionModel().setSelectionInterval(jtable.getRowCount() - 1, jtable.getRowCount() - 1);
                } else if (sel == -1) {
                    jtable.getSelectionModel().setSelectionInterval(0, 0);
                } else {

                    jtable.getSelectionModel().setSelectionInterval(sel - 1, sel - 1);

                }
                tableLabelData.setText((jtable.getSelectedRow() + 1) + "/" + jtable.getRowCount());


            }
        });



    }
}
